package com.gesis.persistence.imp;

import java.util.List;
import java.util.logging.Level;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.gesis.entity.GesClassification;
import com.gesis.persistence.EntityManagerHelper;
import com.gesis.persistence.IGesClassificationDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * GesClassification entities. Transaction control of the save(), update() and
 * delete() operations must be handled externally by senders of these methods or
 * must be manually added to each of these methods for data to be persisted to
 * the JPA datastore.
 * 
 * @see com.gesis.entity.GesClassification
 * @author MyEclipse Persistence Tools
 */

public class GesClassificationDAO implements IGesClassificationDAO {
	// property constants
	public static final String CLA_DESC_CLASSIFICATION = "claDescClassification";
	public static final String CLA_ESTATE = "claEstate";

	private EntityManager getEntityManager() {
		return EntityManagerHelper.getEntityManager();
	}

	/**
	 * Perform an initial save of a previously unsaved GesClassification entity.
	 * All subsequent persist actions of this entity should use the #update()
	 * method. This operation must be performed within the a database
	 * transaction context for the entity's data to be permanently saved to the
	 * persistence store, i.e., database. This method uses the
	 * {@link javax.persistence.EntityManager#persist(Object)
	 * EntityManager#persist} operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * GesClassificationDAO.save(entity);
	 * EntityManagerHelper.commit();
	 * </pre>
	 * 
	 * @param entity
	 *            GesClassification entity to persist
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void save(GesClassification entity) {
		EntityManagerHelper.log("saving GesClassification instance",
				Level.INFO, null);
		try {
			getEntityManager().persist(entity);
			EntityManagerHelper.log("save successful", Level.INFO, null);
		} catch (RuntimeException re) {
			EntityManagerHelper.log("save failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Delete a persistent GesClassification entity. This operation must be
	 * performed within the a database transaction context for the entity's data
	 * to be permanently deleted from the persistence store, i.e., database.
	 * This method uses the
	 * {@link javax.persistence.EntityManager#remove(Object)
	 * EntityManager#delete} operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * GesClassificationDAO.delete(entity);
	 * EntityManagerHelper.commit();
	 * entity = null;
	 * </pre>
	 * 
	 * @param entity
	 *            GesClassification entity to delete
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void delete(GesClassification entity) {
		EntityManagerHelper.log("deleting GesClassification instance",
				Level.INFO, null);
		try {
			entity = getEntityManager().getReference(GesClassification.class,
					entity.getClaIdClassification());
			getEntityManager().remove(entity);
			EntityManagerHelper.log("delete successful", Level.INFO, null);
		} catch (RuntimeException re) {
			EntityManagerHelper.log("delete failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Persist a previously saved GesClassification entity and return it or a
	 * copy of it to the sender. A copy of the GesClassification entity
	 * parameter is returned when the JPA persistence mechanism has not
	 * previously been tracking the updated entity. This operation must be
	 * performed within the a database transaction context for the entity's data
	 * to be permanently saved to the persistence store, i.e., database. This
	 * method uses the {@link javax.persistence.EntityManager#merge(Object)
	 * EntityManager#merge} operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * entity = GesClassificationDAO.update(entity);
	 * EntityManagerHelper.commit();
	 * </pre>
	 * 
	 * @param entity
	 *            GesClassification entity to update
	 * @return GesClassification the persisted GesClassification entity
	 *         instance, may not be the same
	 * @throws RuntimeException
	 *             if the operation fails
	 */
	public GesClassification update(GesClassification entity) {
		EntityManagerHelper.log("updating GesClassification instance",
				Level.INFO, null);
		try {
			GesClassification result = getEntityManager().merge(entity);
			EntityManagerHelper.log("update successful", Level.INFO, null);
			return result;
		} catch (RuntimeException re) {
			EntityManagerHelper.log("update failed", Level.SEVERE, re);
			throw re;
		}
	}

	public GesClassification findById(Long id) {
		EntityManagerHelper.log("finding GesClassification instance with id: "
				+ id, Level.INFO, null);
		try {
			GesClassification instance = getEntityManager().find(
					GesClassification.class, id);
			return instance;
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Find all GesClassification entities with a specific property value.
	 * 
	 * @param propertyName
	 *            the name of the GesClassification property to query
	 * @param value
	 *            the property value to match
	 * @return List<GesClassification> found by query
	 */
	@SuppressWarnings("unchecked")
	public List<GesClassification> findByProperty(String propertyName,
			final Object value) {
		EntityManagerHelper.log(
				"finding GesClassification instance with property: "
						+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			final String queryString = "select model from GesClassification model where model."
					+ propertyName + "= :propertyValue";
			Query query = getEntityManager().createQuery(queryString);
			query.setParameter("propertyValue", value);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public List<GesClassification> findByClaDescClassification(
			Object claDescClassification) {
		return findByProperty(CLA_DESC_CLASSIFICATION, claDescClassification);
	}

	public List<GesClassification> findByClaEstate(Object claEstate) {
		return findByProperty(CLA_ESTATE, claEstate);
	}

	/**
	 * Find all GesClassification entities.
	 * 
	 * @return List<GesClassification> all GesClassification entities
	 */
	@SuppressWarnings("unchecked")
	public List<GesClassification> findAll() {
		EntityManagerHelper.log("finding all GesClassification instances",
				Level.INFO, null);
		try {
			final String queryString = "select model from GesClassification model";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find all failed", Level.SEVERE, re);
			throw re;
		}
	}

}